<template>
	<view class="fenxiao-goods" :data-theme="themeStyle">
		<image class="fx_adv" :src="$util.img('upload/uniapp/fenxiao/fx_adv.png')" mode="widthFix"></image>
		<mescroll-uni ref="mescroll" @getData="getData" top="320" :size="5">
			<view class="goods" slot="list">
				
				<ns-goods-item-row :value="goodsList" type="fenxiaoMarket" @toDetail="toGoodsDetail"></ns-goods-item-row>
				<view class="cart-empty">
					<ns-empty v-if="emptyShow&&goodsList.length==0" :isIndex="!1" :fixed="!1"></ns-empty>
				</view>
			</view>
		</mescroll-uni>
		<ns-show-toast></ns-show-toast>
	</view>
</template>

<script>
	import fenxiaoWords from 'common/js/fenxiao-words.js';
	import MescrollMixin from '@/components/mescroll/mescroll-mixins.js';
	import globalConfig from '@/common/js/golbalConfig.js'
	import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue'
	import nsGoodsItemRow from '@/components/ns-goods-item/ns-goods-item-row.vue'
	export default {
		data() {
			return {
				goodsList: [],
				emptyShow: false,
				searchText: '',
				account: 0,
				memberImg: "",
				memberName: ''
			};
		},
		components: {
			nsShowToast,
			nsGoodsItemRow
		},
		onLoad() {},
		mixins: [fenxiaoWords, MescrollMixin,globalConfig],
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();

			uni.setNavigationBarTitle({
				title: this.fenxiaoWords.concept + '商品'
			});

			if (uni.getStorageSync('token')) {
				this.getMemberInfo();
			} else {
				this.$util.redirectTo(
					'/pages/login/login/login', {
						back: '/otherpages/fenxiao/goods/goods'
					},
					'redirectTo'
				);
			}
		},
		methods: {
			//获得列表数据
			getData(mescroll) {
				this.emptyShow = false;
				if (mescroll.num == 1) {
					this.withdrawList = []
				}
				this.$api.sendRequest({
					url: '/fenxiao/api/goodscollect/page',
					data: {
						page_size: mescroll.size,
						page: mescroll.num,
						keyword: this.searchText.trim()
					},
					success: res => {
						this.emptyShow = true;
						let newArr = [];
						let msg = res.message;
						if (res.code == 0 && res.data && res.data.list) {
							newArr = res.data.list;
						} else {
							this.$util.showToast({
								title: msg
							});
						}
						mescroll.endSuccess(newArr.length);
						//设置列表数据
						if (mescroll.num == 1) this.goodsList = []; //如果是第一页需手动制空列表
						this.goodsList = this.goodsList.concat(newArr); //追加新数据
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					},
					fail: res => {
						mescroll.endErr();
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			//搜索
			search() {
				this.$refs.mescroll.refresh()
			},
			//获取个人信息
			getMemberInfo() {
				this.$api.sendRequest({
					url: '/fenxiao/api/fenxiao/detail',
					data: {},
					success: res => {
						if (res.code == 0 && res.data) {
							this.account = res.data.account;
							if (res.data.headimg) {
								this.memberImg = res.data.headimg
							} else {
								this.memberImg = this.$util.getDefaultImage().default_headimg;
							}
							this.memberName = res.data.fenxiao_name;
						}
					}
				});
			},
			imageError(index) {
				this.goodsList[index].sku_image = this.$util.getDefaultImage().default_goods_img;
				this.$forceUpdate();
			},
			goTixian() {
				this.$util.redirectTo('/otherpages/fenxiao/withdraw_apply/withdraw_apply')
			},
			//跳转至商品详情
			toGoodsDetail(e) {
				this.$util.redirectTo('/pages/goods/detail/detail', {
					sku_id: e.sku_id
				})
			},
			//取消关注
			delFollowTip(e,f) {
				uni.showActionSheet({
					itemList: ['取消关注'],
					success: (res)=> {
						if(res.tapIndex==0){
							this.delFollow(e,f)
						}
					},
					fail: (res)=> {
						
					}
				});
			},
			delFollow(e,f){
				this.$api.sendRequest({
					url: '/fenxiao/api/goodscollect/delete',
					data: {
						collect_id:e
					},
					success: res => {
						let msg="";
						if (res.code == 0) {
							msg="取消成功"
						}else{
							msg=res.message
						}
						this.$util.showToast({
							title:msg
						})
						
						let arr=this.goodsList;
						arr.splice(f,1);
						this.goodsList=arr;
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	/deep/ .fixed{position: relative;top:0}
	/deep/ .empty{margin-top:0 !important}
	.cart-empty{position:relative;top: 50%;padding-top: 54px !important;margin: auto;}
	.fenxiao-header {
	}

	.fx_adv {
		width: 100%;
		display: block;
	}
	.empty-box{
		margin-top: 150rpx;
	}
</style>
